package similarity;

import java.util.ArrayList;

public class SimilarityMain {

	float sim(ArrayList<Float> va, ArrayList<Float> vb) {
		// 濡傛灉鍚戦噺缁村害涓嶇浉绛夛紝鍒欎笉鑳借绠楋紝鍑芥暟閫�鍑�
		if (va.size() != vb.size()) {
			return 0;
		}

		float num = 0;// numerator鍒嗗瓙
		float den = 0;// denominator鍒嗘瘝
		float den1 = 0;
		float den2 = 0;

		for (int i = 0; i < va.size(); i++) {
			num += va.get(i) * vb.get(i);
			den1 += Math.pow(va.get(i), 2);
			den2 += Math.pow(vb.get(i), 2);
		}
		den = (float) (Math.sqrt(den1) * Math.sqrt(den2));

		return num / den;
	}

	public static void main(String[] args) {
		String item[] = { "鍚冭嫻鏋�", "閫涘晢搴�", "鐪嬬數瑙嗗墽", "鎵撶窘姣涚悆", "鍚冩瀛�" };
		float a[] = { (float) 3.5, 5, 5, 5, 0 };
		float b[] = { 3, 1, 2, 1, 2 };
		ArrayList<String> vitem = new ArrayList<String>();
		ArrayList<Float> va = new ArrayList<Float>();
		ArrayList<Float> vb = new ArrayList<Float>();
		for (int i = 0; i < a.length; i++) {
			vitem.add(item[i]);
			va.add(new Float(a[i]));
			vb.add(new Float(b[i]));
		}
		System.out.print("鍏磋叮");
		System.out.println(vitem);
		System.out.print("灏忕孩");
		System.out.println(va);
		System.out.print("椹．");
		System.out.println(vb);

		SimilarityMain sim = new SimilarityMain();

		float simVal = sim.sim(va, vb);

		System.out.println("The sim value is:" + simVal);
	}

}
